﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using ExtendBLL;

namespace LabMS.Web.PersonalProject
{
    public partial class PreviewPersonalProject : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPageUserType.Add("student");
            if (Request.QueryString["ProjectID"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            int ProjectID = 0;
            if (!int.TryParse(Request.QueryString["ProjectID"], out ProjectID))
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            if (!IsPostBack)
            {
                LabMS.BLL.PersonalProject project = new LabMS.BLL.PersonalProject();
                LabMS.Model.PersonalProject projectInfo = new LabMS.Model.PersonalProject();
                projectInfo = project.GetModel(ProjectID);
                string status = projectInfo.ProjectStatus;
                iniBtn(status);

                LabDropdownlist();
                BindApplicantList(ProjectID);
                BindEquipList(ProjectID);
                BindData(ProjectID);
                tbID.Value = ProjectID.ToString();
            }
        }

        void iniBtn(string status)
        {
            btnApp.Visible = false;
            btnDel.Visible = false;
            btnEdit.Visible = false;
            btnCancelApp.Visible = false;

            ProjectPreview.Visible = false;
            ApprovalProjectPreview.Visible = false;

            if (status.Equals("1"))
            {
                btnApp.Visible = true;
                btnEdit.Visible = true;
                btnDel.Visible = true;
            }
            if (status.Equals("2"))
            {
                btnCancelApp.Visible = true;
                ProjectPreview.Visible = false;    
            }
            if (status.Equals("3"))
            {
                btnEdit.Visible = true;
                btnDel.Visible = true;
                ProjectPreview.Visible = true;
            }
            if (status.Equals("4"))
            {
                ProjectPreview.Visible = true;
                ApprovalProjectPreview.Visible = true;
            }    
        }

        //绑定所属实验室
        protected void LabDropdownlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> LabTemp = new List<LabMS.Model.Lab>();

            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            LabTemp = lab.GetModelList("Lab_ParentID is null");

            int count = ParLab.Count, count1 = SonLab.Count;
            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                int m = 0;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        LabTemp.Insert(i + m + 1, labModel);
                        m++;
                    }
                }
            }
            ddlApplyLab.DataSource = LabTemp;
            ddlApplyLab.DataBind();
            ListItem item = new ListItem("无", "0");
            ddlApplyLab.Items.Insert(0, item);
        }

        //绑定实验室用房
        protected void LocationDropdownList(int LabID)
        {
            LabMS.BLL.Location location = new LabMS.BLL.Location();
            string strWhere = " LabID=" + LabID;
            DataSet ds = new DataSet();

            ds = location.GetList(strWhere);
            ddlLocation.DataSource = ds;
            ddlLocation.DataBind();
            ddlLocation.Items.Insert(0, new ListItem("", "0"));
        }
        //绑定申请人
        protected void BindApplicantList(int ProjectID)
        {
            ExtendBLL.PersonalProject personalproject = new ExtendBLL.PersonalProject();
            List<ExtendModule.StudentAndOther> applicantList = new List<ExtendModule.StudentAndOther>();

            string strWhere = " pa.PersonalProjID = " + ProjectID;
            applicantList = personalproject.GetStudentInfoList(strWhere);
            gvApplicant.DataSource = applicantList;
            gvApplicant.DataBind();
        }
        //绑定仪器及低耗品列表
        protected void BindEquipList(int ProjectID)
        {
            LabMS.BLL.PersonalProjConsume consume = new LabMS.BLL.PersonalProjConsume();
            DataSet equipList = new DataSet();
            DataSet consumeList = new DataSet();

            string strWhere1 = " EquipType=1 and PersonalProjID= " + ProjectID;
            string strWhere2 = " EquipType=2 and PersonalProjID= " + ProjectID;

            equipList = consume.GetList(strWhere1);
            gvEquip.DataSource = equipList;
            gvEquip.DataBind();

            consumeList = consume.GetList(strWhere2);
            gvLowConsumption.DataSource = consumeList;
            gvLowConsumption.DataBind();
        }

        //绑定申请信息
        protected void BindData(int ProjectID)
        {
            LabMS.BLL.PersonalProject perProject = new LabMS.BLL.PersonalProject();
            LabMS.Model.PersonalProject perProjectInfo = new LabMS.Model.PersonalProject();

            perProjectInfo = perProject.GetModel(ProjectID);
            ProjectName.Text = perProjectInfo.ProjectName;
            PlanHours.Text = perProjectInfo.PlanHours.ToString();
            ddlApplyLab.SelectedValue = perProjectInfo.AppcationLabID.ToString();
            if (!string.IsNullOrEmpty(perProjectInfo.ApplicationStartTime.ToString()))
            {
                ApplyTimeMin.Text = perProjectInfo.ApplicationStartTime.Value.ToShortDateString();
            }
            if (!string.IsNullOrEmpty(perProjectInfo.ApplicationEndTime.ToString()))
            {
                ApplyTimeMax.Text = perProjectInfo.ApplicationEndTime.Value.ToShortDateString();
            }

            string strSemester = Common.Status.GetSemester(perProjectInfo.Semester.Value);
            lbSTime.Text = perProjectInfo.SYear.Value.ToString() + "学年 " + strSemester;
            lbStatus.Text = Common.Status.GetPersonalProjectStatus(perProjectInfo.ProjectStatus.ToString());

            Contactor.Text = perProjectInfo.Contactor;
            ContactorClass.Text = perProjectInfo.ContactorClass;
            ContactEmail.Text = perProjectInfo.ContactorEmail;
            ContactPhone.Text = perProjectInfo.ContactorPhone;
            Teacher.Text = perProjectInfo.Teacher;
            TeacherTitle.Text = perProjectInfo.TeacherTitle;
            TeacherEmail.Text = perProjectInfo.TeacherEmail;
            TeacherPhone.Text = perProjectInfo.TeacherPhone;
            ApplyReason.Text = perProjectInfo.ApplicationReason;
            Opinion.Text = perProjectInfo.Opinion;
            if (!string.IsNullOrEmpty(perProjectInfo.ProjectStartTime.ToString()))
            {
                LabDate.Text = perProjectInfo.ProjectStartTime.Value.ToShortDateString();
                LabStartTimeHour.Text = perProjectInfo.ProjectStartTime.Value.Hour.ToString();
                if (perProjectInfo.ProjectStartTime.Value.Minute > 10)
                {
                    LabStartTimeMinute.Text = perProjectInfo.ProjectStartTime.Value.Minute.ToString();
                }
                else
                {
                    LabStartTimeMinute.Text = "0" + perProjectInfo.ProjectStartTime.Value.Minute.ToString();
                }
            }
            if (!string.IsNullOrEmpty(perProjectInfo.ProjectEndTime.ToString()))
            {
                LabEndTimeHour.Text = perProjectInfo.ProjectEndTime.Value.Hour.ToString();
                if (perProjectInfo.ProjectEndTime.Value.Minute > 10)
                {
                    LabEndTimeMinute.Text = perProjectInfo.ProjectEndTime.Value.Minute.ToString();
                }
                else
                {
                    LabEndTimeMinute.Text = "0" + perProjectInfo.ProjectEndTime.Value.Minute.ToString();
                }
            }

            int LabID = int.Parse(perProjectInfo.AppcationLabID.ToString());
            LocationDropdownList(LabID);
            ddlLocation.SelectedValue = perProjectInfo.ProjectLabID.ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int ProjectID = int.Parse(Request.QueryString["ProjectID"]);
            string strAlert = "删除成功!";
            try
            {
                LabMS.BLL.PersonalProject project = new LabMS.BLL.PersonalProject();
                project.Delete(ProjectID);

                ExtendBLL.PersonalProject consume = new ExtendBLL.PersonalProject();
                consume.DeleteConsumeByProjectID(ProjectID);

                ExtendBLL.PersonalProject applicant = new ExtendBLL.PersonalProject();
                applicant.DeleteApplicantByProjectID(ProjectID);
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
                return;
            }

            Common.JShelper.JSAlertAndRedirect(Page, "success", strAlert, "ListPersonalProject.aspx");
        }

        protected void btnApp_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string status = "";
            string strAlert = "";
            if (btn.ID == "btnCancelApp")
            {
                status = "1";
                strAlert = "撤销申请成功!";
            }
            if (btn.ID == "btnApp")
            {
                status = "2";
                strAlert = "申请成功,请等待审核!";
            }

            int ProjectID = int.Parse(Request.QueryString["ProjectID"]);
            ExtendBLL.PersonalProject project = new ExtendBLL.PersonalProject();
            try
            {
                project.UpdateStatus(status, ProjectID);
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
                return;
            }
            Common.JShelper.JSAlertAndRedirect(Page, "success", strAlert, "PreviewPersonalProject.aspx?ProjectID=" + ProjectID);
        }
    }
}
